package za.co.dylanjaneke.airdownloader.model.vo
{
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;

	/**
	 * Author: Dylan Janeke.
	 */
	[Bindable]
	public class FileDownloadVO implements DBVO
	{
		public var id:Number;
		public var url:String;
		public var site:String;
		public var linkName:String;
		public var paused:Boolean;
		public var trashed:Boolean;
		public var fileName:String;
		public var order:Number;
		
		public function FileDownloadVO()
		{
				
		}
		
		public function get createQuery():String
		{
			return "CREATE TABLE IF NOT EXISTS ad_file_dl (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT, site TEXT, link_name TEXT, paused BOOLEAN, trashed BOOLEAN, file_name TEXT, display_order INTEGER)";
		}
		
		public static function listValue(event:SQLEvent): ArrayCollection
		{
			var statement:SQLStatement= event.currentTarget as SQLStatement;
			var result:SQLResult= statement.getResult();
			if(result==null||result.data==null)
				return null
				
			var col:ArrayCollection= new ArrayCollection();
			
			for each(var row:Object in result.data)
			{
				var vo:FileDownloadVO= new FileDownloadVO();
				vo.id= row.id;
				vo.url= row.url;
				vo.site= row.site;
				vo.linkName= row.link_name;
				vo.paused= row.paused;
				vo.trashed= row.trashed;
				vo.fileName= row.file_name;
				vo.order= row.display_order;
				
				col.addItem(vo);
			}
			
			return col;
		}
		
		public static function createListActive(statement:SQLStatement): SQLStatement
		{
			statement.text= "SELECT * FROM ad_file_dl WHERE trashed = FALSE";
			return statement;
		}
		
		public static function createInsert(vo:FileDownloadVO, statement:SQLStatement): SQLStatement
		{
			statement.text= "INSERT INTO ad_file_dl (url, site, link_name, paused, trashed, file_name, display_order) VALUES (:url,:site,:linkName,:paused,:trashed,:fileName,:order)";
			statement.parameters[":url"]= vo.url;
			statement.parameters[":site"]= vo.site;
			statement.parameters[":linkName"]= vo.linkName;
			statement.parameters[":paused"]= vo.paused;
			statement.parameters[":trashed"]= vo.trashed;
			statement.parameters[":fileName"]= vo.fileName;
			statement.parameters[":order"]= vo.order;
			
			return statement;
		}
		
		public static function createUpdate(vo:FileDownloadVO, statement:SQLStatement): SQLStatement
		{
			statement.text = "UPDATE ad_file_dl SET url=:url,site=:site,link_name=:linkName,paused=:paused,trashed=:trashed,file_name=:fileName,display_order=:order WHERE id=:id)";
			statement.parameters[":url"]= vo.url;
			statement.parameters[":site"]= vo.site;
			statement.parameters[":linkName"]= vo.linkName;
			statement.parameters[":paused"]= vo.paused;
			statement.parameters[":trashed"]= vo.trashed;
			statement.parameters[":fileName"]= vo.fileName;
			statement.parameters[":order"]= vo.order;
			statement.parameters[":id"]= vo.id;
		
			return statement;
		}
		
		public static function createDelete(vo:DownloadSiteVO, statement:SQLStatement): SQLStatement
		{
			statement.text ="DELETE FROM ad_file_dl WHERE id= :id";
			statement.parameters[":id"]= vo.id;
			
			return statement;
		}
		
		public static function createTrashAll(statement:SQLStatement): SQLStatement
		{
			statement.text= "UPDATE ad_file_dl SET trashed=true";
			
			return statement;
		}
	}
}